package pile.customer.service.impl;

import pile.customer.entity.QuickReply;
import pile.customer.mapper.QuickReplyMapper;
import pile.customer.service.IQuickReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pile.customer.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import pile.customer.utils.WebUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pile.customer.entity.PageList;
import org.apache.commons.lang.StringUtils;
import java.util.Arrays;
import java.util.List;

import pile.customer.params.quickReply.DeleDTO;
import pile.customer.params.quickReply.ListDTO;

import java.util.stream.Collectors;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭天义
 * @since 2024-07-09
 */
@Service
public class QuickReplyServiceImpl extends ServiceImpl<QuickReplyMapper, QuickReply> implements IQuickReplyService {

    @Autowired
    private QuickReplyMapper quickReplyMapper;

   @Override
   public ResponseResult listPage(ListDTO listDTO) {
        IPage iPage=new Page(listDTO.getPage(),listDTO.getLimit());
        QueryWrapper queryWrapper=new QueryWrapper();
        if (StringUtils.isNotBlank(listDTO.getSearchField()) && StringUtils.isNotBlank(listDTO.getSearchValue())) {
            // TODO
            String[] arr = new String[]{"adminRoleId", "isDisable"};
            if (Arrays.asList(arr).contains(listDTO.getSearchField())) {
                //等于判断
                queryWrapper.eq(WebUtils.humpToLine(listDTO.getSearchField()), listDTO.getSearchValue());
            } else {
            //like判断
                queryWrapper.like(WebUtils.humpToLine(listDTO.getSearchField()), listDTO.getSearchValue());
            }
        }
        if (null!=listDTO.getDateValue() && listDTO.getDateValue().size()!=0) {
              queryWrapper.ge(WebUtils.humpToLine(listDTO.getDateField()),listDTO.getDateValue().get(0)+ " 00:00:00");
              queryWrapper.le(WebUtils.humpToLine(listDTO.getDateField()),listDTO.getDateValue().get(1)+ " 23:59:59");
        }

        if (StringUtils.isNotBlank(listDTO.getSortField())) {
            queryWrapper.orderBy(true,"asc".equals(listDTO.getSortValue()),WebUtils.humpToLine(listDTO.getSortField()));
        }

        IPage data = quickReplyMapper.selectPage(iPage, queryWrapper);
        PageList pageList=new PageList();
        pageList.setCount(data.getTotal());
        pageList.setLimit(data.getSize());
        pageList.setList(data.getRecords());
        pageList.setPage(data.getCurrent());
        pageList.setPages(data.getPages());

        return new ResponseResult(pageList);
   }

   @Override
   public ResponseResult dele(DeleDTO deleDTO) {
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.in(WebUtils.humpToLine("quickReplyId"),deleDTO.getIds());
        quickReplyMapper.delete(updateWrapper);
        return new ResponseResult(200,"删除成功");
   }

   @Override
   public ResponseResult add(QuickReply quickReply) {
        quickReplyMapper.insert(quickReply);
        return new ResponseResult(200,"新增成功");
   }

   @Override
   public ResponseResult info(Integer quickReplyId) {
        QuickReply quickReply= quickReplyMapper.selectById(quickReplyId);
        return new ResponseResult(quickReply);
   }

   @Override
   public ResponseResult edit(QuickReply quickReply) {
        quickReplyMapper.updateById(quickReply);
        return new ResponseResult(200,"更新成功");
   }

    @Override
    public ResponseResult listToTree(ListDTO listDTO) {
       //查询所有数据
        List<QuickReply> list = quickReplyMapper.selectList(null);

        List<QuickReply> quickReplies = quickReplyListToTree(list, 0);

        return new ResponseResult(quickReplies);
    }


    private List<QuickReply> quickReplyListToTree(List<QuickReply> list, Integer rootParentId) {

        List<QuickReply> collect = list.stream()
                .filter(treeNodeDTO -> treeNodeDTO.getGroupId().equals(rootParentId))
                .peek(treeNodeDTO -> treeNodeDTO.setChildren(quickReplyListToTree(list, treeNodeDTO.getQuickReplyId())))
                .collect(Collectors.toList());
        return collect;
    }
}
